package org.lanqiao.tjut.Controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.lanqiao.tjut.Bean.TeacherBean;
import org.lanqiao.tjut.MyUtils.MyUtils;
import org.lanqiao.tjut.Service.ServiceFactory;

public class TeaInfoAddServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 设置编码
		// request.setCharacterEncoding("UTF-8");

		// 1、获取表单数据
		// 2、将保单数据填充到实体类
		TeacherBean teaB = (TeacherBean) MyUtils.getNewInstance(TeacherBean.class);

		// id
		teaB.setTeaid(MyUtils.obj2Integer(request.getParameter("txtTeaId")));
		teaB.setTeaname(request.getParameter("txtTeaName"));
		teaB.setTeaage(MyUtils.obj2Int(request.getParameter("txtTeaAge")));
		teaB.setTeaaddress(request.getParameter("txtTeaAddress"));
		teaB.setTeapsw(request.getParameter("txtTeaPsw"));
		teaB.setTeasex(request.getParameter("selectTeaSex"));
		teaB.setTeatel(request.getParameter("txtTeaTel"));
		// 3、转发Model层进行业务逻辑处理
		int i = ServiceFactory.getTeacherInfoManagerServiceInstance().saveTeaInfo(teaB);
		// 4、根据处理结果进行view层转发，将结果显示给用户客户端
		if (i > 0) {
			// 数据保存成功
			// 准备一下在添加成功的页面上进行数据显示
			// 获取需要显示的所有的数据集合
			List<TeacherBean> lstTea = ServiceFactory.getTeacherInfoManagerServiceInstance().getAllTeaInfo();
			// 将需要显示的结果集放入session缓存中
			request.getSession().setAttribute("lstTea", lstTea);
			// 清空request缓存中的对象
			request.removeAttribute("teaB");
			// 转发到结果显示页面
			request.getRequestDispatcher("logined/UserInfoManager/teaInfoDetail.jsp").forward(request, response);
		} else {
			// 显示错误信息提示页面
			request.getSession().setAttribute("pageErrorInfo", "用户信息添加失败~~~");
			response.sendRedirect("logined/error.jsp");
		}
	}
}
